<?php
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 86400);
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';

// Instantiate the application.
$app = JFactory::getApplication('site');

// Initialise the application.
$app->initialise();


require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'utilities'.DS.'reportProvider.php';
require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'utilities'.DS.'pointProvider.php';
require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'PHPExcel.php';

// Init excel
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->mergeCells('B1:Q1');
$activeSheet->setCellValue('B1', 'Accounting Redemption Detail Report - On Voucher');
$activeSheet->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle('B1')->getFont()->setSize(24);
$activeSheet->getStyle('B1')->getFont()->setBold(true);

/************** FILL HEADER **************/
$index = 6;
$activeSheet->setCellValue('A'.$index, 'No');
$activeSheet->setCellValue('B'.$index, 'Item');
$activeSheet->setCellValue('C'.$index, 'Redemption Code');
$activeSheet->setCellValue('D'.$index, 'Voucher ID');
$activeSheet->setCellValue('E'.$index, 'Date use voucher');
$activeSheet->setCellValue('F'.$index, 'Expired Voucher');
$activeSheet->setCellValue('G'.$index, 'Outlet use Voucher');
$activeSheet->setCellValue('H'.$index, 'Member ID');
$activeSheet->setCellValue('I'.$index, 'Full Name');
$activeSheet->setCellValue('J'.$index, 'Point Redemption');
$activeSheet->setCellValue('K'.$index, 'Cash Redemption');
$activeSheet->setCellValue('L'.$index, 'Retail Pricing');
$activeSheet->setCellValue('M'.$index, 'Cost Pricing');
$activeSheet->setCellValue('N'.$index, 'Outlet');
$activeSheet->setCellValue('O'.$index, 'Percent');
$activeSheet->setCellValue('P'.$index, 'Charge Cost');
$activeSheet->setCellValue('Q'.$index, 'Redemption date');

$cols = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P','Q');
$fill = $activeSheet->getStyle('A6:Q6')->getFill();
$fill->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$fill->getStartColor()->setARGB('cc99ff');

foreach($cols as $item)
{
    if($item == 'A')
    {
        setBackgroundColor($activeSheet, $item, 6, 5);
    }
	else if($item == 'B')
    {
        setBackgroundColor($activeSheet, $item, 6, 30);
    }
	else if($item == 'C')
    {
        setBackgroundColor($activeSheet, $item, 6, 25);
    }
	else if($item == 'D')
    {
        setBackgroundColor($activeSheet, $item, 6, 20);
    }
	else if($item == 'E')
    {
        setBackgroundColor($activeSheet, $item, 6, 18);
    }
	else if($item == 'F')
    {
        setBackgroundColor($activeSheet, $item, 6, 15);
    }
	else if($item == 'G')
    {
        setBackgroundColor($activeSheet, $item, 6, 35);
    }
    else if($item == 'H')
    {
        setBackgroundColor($activeSheet, $item, 6, 15);
    }
    else if($item == 'I')
    {
        setBackgroundColor($activeSheet, $item, 6, 24);
    }
	else if($item == 'J')
    {
        setBackgroundColor($activeSheet, $item, 6, 18);
    }
	else if($item == 'K')
    {
        setBackgroundColor($activeSheet, $item, 6, 15);
    }
	
    else if($item == 'L')
    {
        setBackgroundColor($activeSheet, $item, 6, 15);   
    }
	else if($item == 'M')
    {
        setBackgroundColor($activeSheet, $item, 6, 35);   
    }
	else if($item == 'N')
    {
        setBackgroundColor($activeSheet, $item, 6, 15);   
    }
	else if($item == 'O')
    {
        setBackgroundColor($activeSheet, $item, 6, 20);   
    }
	else if($item == 'P')
    {
        setBackgroundColor($activeSheet, $item, 6, 25);   
    }
}

// End init excel

/******** Query Data ********/  
$db = JFactory::getDbo();
$conditions = array();
$conditions['dateFrom'] = strval($_GET['df']);
$conditions['dateTo'] = strval($_GET['dt']);
$conditions['dateFromredem'] = strval($_GET['dfr']);
$conditions['dateToredem'] = strval($_GET['dtr']);
$conditions['status'] = strval($_GET['s']);
$conditions['outlet'] = strval($_GET['o']);
//echo '<pre>';print_r($conditions);die;

$data = reportProvider::redemptionreportvoucher($conditions, $db, intval($_GET['l']));
//echo "<pre>";print_r($data);exit;
if(!empty($data))
{
    $activeSheet->setCellValue('B3', 'From: ');
	$activeSheet->setCellValue('B4', 'To: ');
	$activeSheet->setCellValue('C3', $conditions['dateFrom']);
	$activeSheet->setCellValue('C4', $conditions['dateTo']);
    
    /*********** BIND TO EXCEL ***********/
    $index = 7;
    $data = $data['items'];
    $size = count($data);
	
    for($i=0; $i<$size; $i++)
    {
		
		
		$activeSheet->setCellValue('A'.$index, $i+1); 
        
		$productName = unserialize($data[$i]->productName);
        $activeSheet->setCellValue('B'.$index, $productName["en-GB"]);    
        $activeSheet->getStyle('B'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        
        $activeSheet->setCellValue('C'.$index, $data[$i]->code_redemption);                
        
		$activeSheet->setCellValue('D'.$index, $data[$i]->code_voucher);                
        $activeSheet->getStyle('D'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        
                     
        $activeSheet->setCellValue('E'.$index,strtotime($data[$i]->date_voucher) ? date("d-m-Y",strtotime($data[$i]->date_voucher)) : "" );                
        $activeSheet->getStyle('E'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        
		$activeSheet->setCellValue('F'.$index,strtotime($data[$i]->expired) ? date("d-m-Y",strtotime($data[$i]->expired)) : "");                
        $activeSheet->getStyle('F'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$activeSheet->setCellValue('G'.$index, $data[$i]->outlet_name_use_voucher);
        $activeSheet->setCellValue('H'.$index, "8800".$data[$i]->member_id."01");
		$objPHPExcel->getActiveSheet()->getStyle('H'.$index)->getNumberFormat()->setFormatCode("0");	
        $activeSheet->getStyle('H'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
                 
        $activeSheet->setCellValue('I'.$index,$data[$i]->l_name ." ".$data[$i]->f_name);
        $activeSheet->getStyle('I'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        //$objPHPExcel->getActiveSheet()->getStyle('I'.$index)->getNumberFormat()->setFormatCode("#,##0");
		
		$activeSheet->setCellValue('J'.$index,$data[$i]->point);
        $activeSheet->getStyle('J'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('J'.$index)->getNumberFormat()->setFormatCode("#,##0");
		
		$activeSheet->setCellValue('K'.$index,$data[$i]->cash);
        $activeSheet->getStyle('K'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('K'.$index)->getNumberFormat()->setFormatCode("#,##0");
		
		$activeSheet->setCellValue('L'.$index,$data[$i]->product_cost);
        $activeSheet->getStyle('L'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('L'.$index)->getNumberFormat()->setFormatCode("#,##0");
		

		
		
		$activeSheet->setCellValue('M'.$index,$data[$i]->cost_pricing);
        $activeSheet->getStyle('M'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		$objPHPExcel->getActiveSheet()->getStyle('M'.$index)->getNumberFormat()->setFormatCode("#,##0");
		
		$activeSheet->setCellValue('N'.$index,$data[$i]->outletName);
        $activeSheet->getStyle('N'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        
		$activeSheet->setCellValue('O'.$index,$data[$i]->percent/100);
        $activeSheet->getStyle('O'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('O'.$index)->getNumberFormat()->setFormatCode("0.00%");
		
		$activeSheet->setCellValue('P'.$index,$data[$i]->charge_cost);
        $activeSheet->getStyle('P'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('P'.$index)->getNumberFormat()->setFormatCode("#,##0");
		
		$activeSheet->setCellValue('Q'.$index,$data[$i]->exchange_date);
        //$activeSheet->getStyle('P'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        
		$index++;

    }
    unset($data);
}

$fileName = 'redemption_report_voucher_'.date('m-d-Y');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(JPATH_BASE.DS.'tmp'.DS.$fileName.'.xlsx');

$fullPath = JPATH_ROOT.DS.'tmp'.DS.$fileName.'.xlsx';
$fsize = filesize($fullPath);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$fileName.'.xlsx"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $fsize);
ob_clean();
flush();
readfile( $fullPath );
exit;          


function setBackgroundColor(&$activeSheet, $colName, $colIndex, $width=15,$bg="cc99ff")
{
      
    $activeSheet->getColumnDimension($colName)->setWidth($width);    
    $style = $activeSheet->getStyle($colName.$colIndex);
    $style->getFont()->setSize(12);
    $style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
}
